package com.masterclient.app.modules.search.contract;

import android.widget.Toast;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.modules.search.SearchBrandBean;
import com.masterclient.app.modules.search.SearchShopBean;
import com.eoner.baselibrary.utils.Utils;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 作者：sean
 * 创建时间：2017/6/25 23:56
 * 功能说明：
 */

public class ShopResultPresenter extends ShopResultContract.Presenter {
    /**
     * 搜索店铺品牌
     * @param keyword
     */
    @Override
    public void searchShopList(String keyword) {
        Observable<SearchBrandBean> observable = RetrofitUtil.getInstance().getSearchBrandList(keyword);
        Disposable disposable = toSubscribe(observable, new Consumer<SearchBrandBean>() {
            @Override
            public void accept(@NonNull SearchBrandBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showSearchBrand(bean.getData());
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
            }
        });

        mRxManager.add(disposable);
    }

    @Override
    public void searchShopList(String keyword, String page, String page_size) {
        Observable<SearchShopBean> observable = RetrofitUtil.getInstance().getSearchSellerList(keyword,page,page_size);
        Disposable disposable = toSubscribe(observable, new Consumer<SearchShopBean>() {
            @Override
            public void accept(@NonNull SearchShopBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showSearchShop(bean.getData());
                }else {
                    mView.loadFailed();
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
            }
        });

        mRxManager.add(disposable);
    }
}
